package com.rzj.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rzj.book.dao.ICategoryDao;
import com.rzj.book.en.Category;
import com.rzj.book.service.ICategoryService;
import com.rzj.book.utils.PageResult;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @description:
 * @author: rzj
 * @time: 2020/6/3 21:25
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private ICategoryDao categoryDao;

    @Override
    public PageResult<Category> getListByCategort(Integer page, Integer size, Category category) {
        PageHelper.startPage(page, size);
        List<Category> categoryList = getListByExample(category);
        //解析分页结果
        PageInfo<Category> info = new PageInfo<Category>(categoryList);
        return new PageResult<Category>(info.getTotal(), info.getList());
    }

    @Override
    public Category getCategoryById(Integer id) {
        Category category = new Category();
        category.setId(id);
        return categoryDao.selectOne(category);
    }

    @Override
    public List<Category> getCategoryCommbox() {
        return categoryDao.selectAll();
    }

    @Override
    public void updateById(Category category) {
        categoryDao.updateById(category);
    }

    @Override
    public void deleteCategory(Integer id) {
        categoryDao.deleteCategory(id);
    }

    @Override
    public void addCategory(Category category) {
        categoryDao.insert(category);
    }

   public List<Category> getListByExample(Category category){
       Example example = new Example(Category.class);
       Example.Criteria criteria = example.createCriteria();
       if(category!= null){
           if(category.getName() !=null && !category.getName().isEmpty()){
               criteria.andLike("name","%"+category.getName()+"%");
           }

           if(category.getType() != null){
               criteria.andEqualTo("type",category.getType());
           }
       }
       List<Category> categoryList = categoryDao.selectByExample(example);
       return categoryList;
   }

    @Override
    public void excelCategory(ServletOutputStream os, Category category) {
       List<Category> list = getListByExample(category);
        //创建工作簿
        HSSFWorkbook wb=new HSSFWorkbook();

        String sheetName="分类";

        //创建一个工作表
        HSSFSheet sheet=wb.createSheet(sheetName);
        //创建一行，从0开始，写标题
        HSSFRow row=sheet.createRow(0);
        String[] header= {"id","分类名称","分类基本","pid"};
        int[] width= {8000,5000,5000,5000};
        HSSFCell cell=null;
        for(int i=0;i<header.length;i++)
        {
            cell=row.createCell(i);
            cell.setCellValue(header[i]);
            //设置列宽
            sheet.setColumnWidth(i, width[i]);
        }

        //导出的内容
        int rowCount=1;
        for(Category item:list)
        {
            row = sheet.createRow(rowCount);
            row.createCell(0).setCellValue(item.getId());
            row.createCell(1).setCellValue(item.getName());
            row.createCell(2).setCellValue(item.getType());
            row.createCell(3).setCellValue(item.getPid() == null ? -1 : item.getPid());
            rowCount++;
        }

        try {
            wb.write(os);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                wb.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}
